package com.example.android.ui.me.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.navigation.Navigation;

import com.example.android.R;
import com.example.android.base.BaseFragment2;
import com.example.android.bean.RegisterUser;
import com.example.android.utils.api.BackApi;
import com.example.android.utils.BaseModel;
import com.example.android.utils.NetUtil;
import com.google.android.material.snackbar.Snackbar;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterFragment extends BaseFragment2 {

    private EditText editText;
    private EditText editText2;
    private EditText editText3;
    private EditText password2;
    private EditText realName;
    private EditText nick;
    private EditText phone;
    private RadioButton radioButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_register, container, false);

        editText = root.findViewById(R.id.editText);
        editText2 = root.findViewById(R.id.editText2);
        editText3 = root.findViewById(R.id.editText3);
        password2 = root.findViewById(R.id.editTextpassword2);
        nick = root.findViewById(R.id.editTextNick);
        realName = root.findViewById(R.id.editTextRealName);
        phone = root.findViewById(R.id.editTextPhone);
        RadioGroup viewById = root.findViewById(R.id.radi);
        radioButton = root.findViewById(viewById.getCheckedRadioButtonId());


        Button button = root.findViewById(R.id.button);
        button.setOnClickListener(this::signUp);

        return root;
    }

    /**
     * 账号密码注册
     */
    private void signUp(final View view) {

        String name = editText.getText().toString();
        String password = editText2.getText().toString();
        String password2x = password2.getText().toString();
        String email = editText3.getText().toString();


        if (TextUtils.isEmpty(name)) {
            editText.setError("帐号不能为空");
            return;
        }

        if (TextUtils.isEmpty(password) || TextUtils.isEmpty(password2x)) {
            editText2.setError("密码不能为空");
            return;
        } else if (!password.equals(password2x)) {
            editText2.setError("密码不一致");
            password2.setError("密码不一致");
        }

        if (TextUtils.isEmpty(email)) {
            editText3.setError("邮箱不能为空");
            return;
        }

        final RegisterUser user = new RegisterUser();
        user.setUsername(name);
        user.setPassword(password);
        user.setEmail(email);
        user.setNickName(nick.getText().toString());
        user.setPhonenumber(phone.getText().toString());
        user.setRealName(realName.getText().toString());
        user.setSex(radioButton.getText().toString().equals("男") ? "0" : "1");
        user.setAvatar("");
        Log.i("注册输出Entry", "：" + user.toString());
        Call<BaseModel<String>> baseModelCall = NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(BackApi.class).registerUser(user);
        baseModelCall.enqueue(new Callback<BaseModel<String>>() {
            @Override
            public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
                Log.i("注册成功信息", ":" + response.toString());
                if (response.body().getCode() == 200) {
                    Snackbar.make(view, "注册成功", Snackbar.LENGTH_LONG).show();
                    Navigation.findNavController(view).navigateUp();
                } else {
                    Snackbar.make(view, response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<BaseModel<String>> call, Throwable t) {
                Log.i("注册输信息", ":" + t.getMessage());
                Snackbar.make(view, "尚未失败：" + t.getMessage(), Snackbar.LENGTH_LONG).show();
            }
        });

    }
}